package jp.co.jmas.jmasvideo;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import jp.co.jmas.jmasvideo.dao.PMF;
import jp.co.jmas.jmasvideo.dao.UserData;
import jp.co.jmas.jmasvideo.dao.Video;

public class Util {

	public static UserData getUserData(String userName) {
		String userAddress = null;
		String userID = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserData.class);
		query.setFilter("name == '" + userName + "'");

		@SuppressWarnings("unchecked")
		List<UserData> list = (List<UserData>) query.execute();

		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public static Video getVideo(String key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Video video = pm.getObjectById(Video.class, key);

		return video;
	}

	public static List<Video> getVideoList() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(Video.class);
		// TODO アクセス権のあるビデオのみに絞り込む！　query.setFilter();
		query.setOrdering("date desc");

		@SuppressWarnings("unchecked")
		List<Video> videoList = (List<Video>) query.execute();
		return videoList;
	}
}
